package com.osce.ex.controller;

import com.csce.domain.entity.exam.ExExamRecord;
import com.csce.domain.entity.exam.vo.AllotExRecord;
import com.csce.domain.entity.exam.vo.RecordExcelVo;
import com.osce.common.annotation.Log;
import com.osce.common.core.controller.BaseController;
import com.osce.common.core.entity.AjaxResult;
import com.osce.common.core.page.TableDataInfo;
import com.osce.common.enums.BusinessType;
import com.osce.common.utils.poi.ExcelUtil;
import com.osce.interfaces.ex.IExExamRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 考试记录Controller
 *
 * @author zzx
 * @since 2023-08-10
 */
@RestController
@RequestMapping("/ex/ex_record")
public class ExExamRecordController extends BaseController {

    @Resource
    private IExExamRecordService exExamRecordService;

    /**
     * 查询考试记录列表
     */
    @PreAuthorize("@ss.hasPermi('ex:ex_record:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExExamRecord exExamRecord) {
        startPage();
        List<ExExamRecord> list = exExamRecordService.selectExExamRecordList(exExamRecord);
        TableDataInfo dataTable = getDataTable(list);
        //检查考试记录
        dataTable.setRows(exExamRecordService.checkExExamRecordList(list));
        return dataTable;
    }

    /**
     * 查询当前用户考试记录列表
     */
    @PreAuthorize("@ss.hasPermi('ex:ex_record:my-list')")
    @GetMapping("/list-my")
    public TableDataInfo listMy(ExExamRecord exExamRecord) {
        exExamRecord.setUserId(getUserId());
        startPage();
        List<ExExamRecord> list = exExamRecordService.selectExExamRecordList(exExamRecord);
        return getDataTable(list);
    }

    /**
     * 根据考试查询已报名的用户id
     *
     * @param examId 考试编号
     */
    @PreAuthorize("@ss.hasPermi('ex:ex_record:list')")
    @GetMapping("/get-appoint-user-ids/{exam_id}")
    public TableDataInfo useridsByExam(@PathVariable("exam_id") Long examId) {
        return getDataTable(exExamRecordService.selectUserIdsByExam(examId));
    }

    /**
     * 获取当前已预约的考试编号数组
     *
     * @return [exam_id]
     */
    @GetMapping("/get-appoint-exam-ids")
    public TableDataInfo appointExamIds() {
        return getDataTable(exExamRecordService.selectAppointExamIds(getUserId()));
    }

    /**
     * 导出考试记录列表
     */
    @PreAuthorize("@ss.hasPermi('ex:ex_record:export')")
    @Log(title = "考试记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExExamRecord exExamRecord) {
        List<RecordExcelVo> list = exExamRecordService.selectExExamRecordExcelList(exExamRecord);
        ExcelUtil<RecordExcelVo> util = new ExcelUtil<>(RecordExcelVo.class);
        util.exportExcel(response, list, "考试记录数据");
    }

    /**
     * 获取考试记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('ex:ex_record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(exExamRecordService.selectExExamRecordById(id));
    }

    /**
     * 新增考试记录
     */
    @PreAuthorize("@ss.hasPermi('ex:ex_record:add')")
    @Log(title = "考试记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExExamRecord exExamRecord) {
        return toAjax(exExamRecordService.insertExExamRecord(exExamRecord));
    }


    /**
     * 批量分配学生
     */
    @PreAuthorize("@ss.hasPermi('ex:ex_record:allot')")
    @Log(title = "考试记录", businessType = BusinessType.INSERT)
    @PostMapping("/allot-users")
    public AjaxResult allotUsers(@Validated @RequestBody AllotExRecord allot) {
        return toAjax(exExamRecordService.allotUsers(allot));
    }

    /**
     * 预约
     */
    @PreAuthorize("@ss.hasPermi('ex:exam:appoint')")
    @Log(title = "预约", businessType = BusinessType.INSERT)
    @PostMapping("/appoint")
    public AjaxResult appoint(@RequestBody ExExamRecord exExamRecord) {
        exExamRecord.setUserId(getUserId());
        exExamRecord.setStatus(1);
        return toAjax(exExamRecordService.insertExExamRecord(exExamRecord));
    }

    /**
     * 修改考试记录
     */
    @PreAuthorize("@ss.hasPermi('ex:ex_record:edit')")
    @Log(title = "考试记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExExamRecord exExamRecord) {
        return toAjax(exExamRecordService.updateExExamRecord(exExamRecord));
    }

    @Log(title = "考试-交卷", businessType = BusinessType.UPDATE)
    @PutMapping("/submit/{id}/{timer}")
    public AjaxResult submit(@PathVariable("id") Long id, @PathVariable("timer") Integer timer, @RequestBody Map<Long, Object> answers) {
        return toAjax(exExamRecordService.submit(id, timer, answers));
    }

    /**
     * 删除考试记录
     */
    @PreAuthorize("@ss.hasPermi('ex:ex_record:remove')")
    @Log(title = "考试记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(exExamRecordService.deleteExExamRecordByIds(ids));
    }
}
